<html>
<head>
  <meta charset="UTF-8">
  <title>usermodel</title>
</head>
<body>


<?php
require 'Common/ConnDB.php';
require 'Common/config.php';
class UserModel{
  private $conn;
     function __construct(){
      //连接数据库
      $this->conn=new ConnDB(DB_TYPE,DB_HOST,DB_USER,DB_PWD,DB_DBNAME,DB_CHARSET);

     }

     function getUserList(){
        //写sql语句

      $sql="select * from users";
      //执行sql语句得到数据
      $data=$this->conn->query($sql);

    return $data;
  }
  function delUser($uid){
    $sql="delete from users where uid=$uid";
    $result=$this->conn->query($sql);
    return $result;
  }
  function adduser($data,$file){
//文件上传
    $arr=explode('.', $file['pic']['name']);
    $suffix=$arr[count($arr)-1];
    $newname=date("YmdHis").rand(100,999).'.'.$suffix;
    $filepath='./photos/';
    move_uploaded_file($file['pic']['tmp_name'], $filepath.$newname);
//把数据插入数据库
    $uname=$data['username'];
    $password=$data['password'];
    $gender=$data['gender'];
    $birthdate=$data['birthdate'];
    $hobby=implode($data['hobby'],'.');
    $degree=$data['degree'];
    $intro=$data['intro'];
    $pic=$newname;
    $sql="insert into users values(null,'$uname',md5('$password'),$gender,'$birthdate','$hobby',$degree,'$intro','$pic')";
    echo $sql;
    $result=$this->conn->query($sql);
    return $result;
  }
  function edituser($uid){
    $sql="select * from users where uid=$uid";
    $data=$this->conn->query($sql);
    return $data;
  }
  function updateuser($data,$file){
    $uid=$data['uid'];
    $username=$data['username'];
    $gender=$data['gender'];
    $birthdate=$data['birthdate'];
    $hobby=implode($data['hobby'], '.');
    $degree=$data['degree'];
    $intro=$data['intro'];
    //如果修改头像，需要上传头像，否则原头像不动
    
    if ($file['pic']['size']==0) {
      $sql="update users set uname='{$username}',birthdate='{$birthdate}',gender={$gender},hobby='{$hobby}',degree={$degree},intro='{$intro}' where uid={$uid}";
      echo $dql;
    }
    else{
            //上传新头像并且根据uid更新数据
            //上传文件}
            $arr=explode('.', $file['pic']['name']);
            $suffix=$arr[count($arr)-1];
            $newname=date("YmdHis").rand(100,999).'.'.$suffix;
            $filepath='./photos/';
            move_uploaded_file($file['pic']['tmp_name'], $filepath.$newname);
          
                $sql="update users set uname='{$username}', 
                   birthdate='{$birthdate}', gender={$gender}, 
                   hobby='{$hobby}', degree={$degree},
                   intro='{$intro}',pic='{$newname}'where uid={$uid}";
            echo $sql;
            
        }
        $result=$this->conn->query($sql);
    return $result;
        
    }
}
?>

</head>
</body>

